package com.meilele.dao.guoziyang;

import com.meilele.entity.guoziyang.SecDetail;
import com.meilele.entity.guoziyang.Seclaber;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface SecDetailMapper {
    @Delete({
        "delete from sec_detail",
        "where style_id = #{styleId,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer styleId);

    @Insert({
        "insert into sec_detail (style_id, style_name, ",
        "style_image, style_company, ",
        "style_detail, style_desc, ",
        "style_designer, style_number)",
        "values (#{styleId,jdbcType=INTEGER}, #{styleName,jdbcType=VARCHAR}, ",
        "#{styleImage,jdbcType=VARCHAR}, #{styleCompany,jdbcType=VARCHAR}, ",
        "#{styleDetail,jdbcType=VARCHAR}, #{styleDesc,jdbcType=VARCHAR}, ",
        "#{styleDesigner,jdbcType=VARCHAR}, #{styleNumber,jdbcType=INTEGER})"
    })
    int insert(SecDetail record);

    /*
    添加一行数据，并返回自增长的id
     */
    int insertSelective(SecDetail record);

    @Select({
        "select",
        "style_id, style_name, style_image, style_company, style_detail, style_desc, ",
        "style_designer, style_number",
        "from sec_detail",
        "where style_id = #{styleId,jdbcType=INTEGER}"
    })
    @ResultMap("com.meilele.dao.guoziyang.SecDetailMapper.BaseResultMap")
    SecDetail selectByPrimaryKey(Integer styleId);

    int updateByPrimaryKeySelective(SecDetail record);

    @Update({
        "update sec_detail",
        "set style_name = #{styleName,jdbcType=VARCHAR},",
          "style_image = #{styleImage,jdbcType=VARCHAR},",
          "style_company = #{styleCompany,jdbcType=VARCHAR},",
          "style_detail = #{styleDetail,jdbcType=VARCHAR},",
          "style_desc = #{styleDesc,jdbcType=VARCHAR},",
          "style_designer = #{styleDesigner,jdbcType=VARCHAR},",
          "style_number = #{styleNumber,jdbcType=INTEGER}",
        "where style_id = #{styleId,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(SecDetail record);

    @Select({
            "select",
            "style_id, style_name, style_image, style_company, style_detail, style_desc, ",
            "style_designer, style_number",
            "from sec_detail"
    })
    @ResultMap("com.meilele.dao.guoziyang.SecDetailMapper.BaseResultMap")
    List<SecDetail> selectAll();

    @Select({
            "select",
            "style_id, style_name, style_image, style_company, style_detail, style_desc, ",
            "style_designer, style_number",
            "from sec_detail",
            "ORDER BY sec_detail.style_name"
    })
    @ResultMap("com.meilele.dao.guoziyang.SecDetailMapper.BaseResultMap")
    List<SecDetail> selectNew();

    @Select({
            "select",
            "style_id, style_name, style_image, style_company, style_detail, style_desc, ",
            "style_designer, style_number",
            "from sec_detail",
            "ORDER BY sec_detail.style_name desc"
    })
    @ResultMap("com.meilele.dao.guoziyang.SecDetailMapper.BaseResultMap")
    List<SecDetail> selectOld();

    @Results(value = {
            @Result(column = "style_id", property = "styleId"),
            @Result(column = "style_name", property = "styleName"),
            @Result(column = "style_image", property = "styleImage"),
            @Result(column = "style_company", property = "styleCompany"),
            @Result(column = "style_detail", property = "styleDetail"),
            @Result(column = "style_desc", property = "styleDesc"),
            @Result(column = "style_designer", property = "styleDesigner"),
            @Result(column = "style_number", property = "styleNumber"),
            @Result(property = "seclaberList" ,column = "style_id" ,javaType = List.class, many = @Many(select = "com.meilele.dao.guoziyang.SecDetailMapper.findById"))
    })
    @Select({
            "SELECT de.*,la.* FROM sec_laber la INNER JOIN \n "+
            "sec_laber_detail lade ON la.style_id = lade.sec_labereID \n "+
            "INNER JOIN sec_detail de ON lade.sec_detailID = de.style_id WHERE la.style_id = #{id}"})
    List<SecDetail> findAll(int id);


    @Results(value = {
            @Result(column = "show_image", property = "showImage")

    })
    @Select({"select l.style_id, l.style_laber from sec_detail d INNER JOIN sec_laber_detail ld ON d.style_id = ld.sec_detailID \n" +
            "INNER JOIN sec_laber l ON ld.sec_labereID = l.style_id WHERE d.style_id = #{style_id}"})
    List<Seclaber> findById(int style);

    @Results(value = {
            @Result(column = "style_id", property = "styleId"),
            @Result(column = "style_name", property = "styleName"),
            @Result(column = "style_image", property = "styleImage"),
            @Result(column = "style_company", property = "styleCompany"),
            @Result(column = "style_detail", property = "styleDetail"),
            @Result(column = "style_desc", property = "styleDesc"),
            @Result(column = "style_designer", property = "styleDesigner"),
            @Result(column = "style_number", property = "styleNumber")
    })
    @Select({"SELECT style_id,style_name,style_image,style_company,style_detail,style_desc,style_designer,style_number FROM sec_detail LIMIT #{number},#{blackGround}"})
    List<SecDetail> findLimint(@Param("number") int number,@Param("blackGround") int blackGround);


    @Select({"SELECT COUNT(style_id) FROM sec_detail"})
    int findCount();
/*
 此方法用来向中间表来加载数据 detail——laber
 */
@Insert({"INSERT INTO sec_laber_detail(sec_labereID,sec_detailID) VALUES(#{laberId},#{detailId})"})
 int insertDeLa(@Param("laberId") int laberId,@Param("detailId") int detailId);

@Delete({"DELETE FROM sec_detail WHERE style_id = #{styleId}"})
    int deleteDetail(@Param("styleId") int styleId);

    @Results(value = {
            @Result(column = "style_id", property = "styleId"),
            @Result(column = "style_name", property = "styleName"),
            @Result(column = "style_image", property = "styleImage"),
            @Result(column = "style_company", property = "styleCompany"),
            @Result(column = "style_detail", property = "styleDetail"),
            @Result(column = "style_desc", property = "styleDesc"),
            @Result(column = "style_designer", property = "styleDesigner"),
            @Result(column = "style_number", property = "styleNumber")
    })
    @Select({"SELECT * FROM sec_detail where style_id = #{styleid}"})
    SecDetail selectById(@Param("styleid") int styleId);

    @Results(value = {
            @Result(column = "style_id" , property = "styleId"),
            @Result(column = "style_laber" , property = "styleLaber")
    })
    @Select({"select l.style_id, l.style_laber from sec_detail d INNER JOIN sec_laber_detail ld ON d.style_id = ld.sec_detailID \n" +
            "INNER JOIN sec_laber l ON ld.sec_labereID = l.style_id WHERE d.style_id = #{style_id}"})
    List<Seclaber> findById2(int style);

    int updateDetail(SecDetail secDetail);

}